

#import "FAMTabBar.h"
#import "FAMTabBaritem.h"
@interface FAMTabBar()
@property (nonatomic, strong)NSMutableArray *FAMTabbarItemArray;
@property (nonatomic, strong)UIButton *FAMPostButton;
@property (nonatomic, strong)FAMTabBaritem * FAMSelectedButton;
@end
@implementation FAMTabBar
- (NSMutableArray *)FAMTabbarItemArray {
    if (!_FAMTabbarItemArray) {
        _FAMTabbarItemArray = [NSMutableArray array];
    }
    return _FAMTabbarItemArray;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self FAM_showUI];
    }
    return self;
}
- (void)FAM_showUI{
    self.backgroundColor = kClearColor;
    self.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"tabbar_backImage"].CGImage);
    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(-10, -10);
    self.layer.shadowRadius = 5.0;
    [self FAM_setUpPostButton];
}
- (void)FAM_setUpPostButton{
    self.FAMPostButton = [UIButton new];
    [self.FAMPostButton setBackgroundImage:[UIImage imageNamed:@"tabbar_camera"] forState:UIControlStateNormal];
    [self.FAMPostButton addTarget:self action:@selector(FAM_postButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.FAMPostButton setTitleColor:[UIColor colorWithRed:117/255.0f green:117/255.0f blue:117/255.0f alpha:1.0] forState:UIControlStateNormal];
    self.FAMPostButton.bounds = CGRectMake(0, 0, self.FAMPostButton.currentBackgroundImage.size.width, self.FAMPostButton.currentBackgroundImage.size.height);
    [self addSubview:self.FAMPostButton];
}
- (void)FAM_postButtonClick:(UIButton *)sender{
    FAMCameraTakeShotViewController *vc = [FAMCameraTakeShotViewController new];
    vc.FAMCateType = @"0";
    [UIWindow.currentViewController.navigationController pushViewController:vc animated:YES];
}

- (void)layoutSubviews {
    [super layer];
    if (isIphoneX) {
        self.FAMPostButton.center = CGPointMake(self.frame.size.width*0.5, 10);
    }else{
        self.FAMPostButton.center = CGPointMake(self.frame.size.width*0.5, -10);
    }
    CGFloat buttonY = 0;
    CGFloat buttonW = self.frame.size.width / self.subviews.count;
    CGFloat buttonH = self.frame.size.height;
    for (int i = 0; i<self.FAMTabbarItemArray.count; i++) {
        CGFloat buttonX = buttonW * i;
        FAMTabBaritem *tabBaritem = self.FAMTabbarItemArray[i];
        if (i > 0) {
            buttonX += buttonW;
        }
        tabBaritem.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        tabBaritem.tag = 1000+i;
    }
}
- (void)FAM_addTabBarButtonWithTabBarItem:(UITabBarItem *)tabBarItem {
    FAMTabBaritem *tabBaritemButton = [FAMTabBaritem new];
    [self addSubview:tabBaritemButton];
    tabBaritemButton.FMATabBarItem = tabBarItem;
    [tabBaritemButton addTarget:self action:@selector(FAM_tabBarBtnClick:) forControlEvents:UIControlEventTouchDown];
    [self.FAMTabbarItemArray addObject:tabBaritemButton];
    if (self.FAMTabbarItemArray.count == 1) {
        [self FAM_tabBarBtnClick:tabBaritemButton];
    }
}
- (void)FAM_tabBarBtnClick:(FAMTabBaritem *)sender{
    if ([self.delegate respondsToSelector:@selector(FAMTabBar:didSelectedButtonFrom:to:)]) {
        [self.delegate FAMTabBar:self didSelectedButtonFrom:self.FAMSelectedButton.tag to:sender.tag];
    }
    self.FAMSelectedButton.selected = NO;
    sender.selected = YES;
    self.FAMSelectedButton = sender;
}
@end
